<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { loginAPI } from '@/apis/user'
import 'element-plus/theme-chalk/el-message.css'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()
const router = useRouter()

// 表单校验
const userInfo = ref({
  account: '',
  password: '',
  agree: false
})
const rules = ref({
  account: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
  ],
  agree: [
      {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
})

// 获取 form 实例做统一校验
const form = ref(null)

// 登录校验
const login = () => {
  const { account, password } = userInfo.value
  form.value.validate(async (valid) => {
    if (valid) {
      // 登录成功后跳转到首页
      await userStore.getUserInfo({ account, password })
      // console.log(res)
      ElMessage({type: 'success', message: '登录成功'})
      router.replace('/')
    }
  })
}
</script>

<template>
  <el-row class="login-page">
    <el-col :span="12" class="bg">
      <div class="title">
        <h1>狐智汇翠，驿达天下</h1>
        <h1>品质生活，从翠狐驿开始。</h1>
      </div>
    </el-col>
    <el-col :span="6" :offset="3" class="form">
      <el-form :model="userInfo" :rules="rules" ref="form" size="large" autocomplete="off">
        <el-form-item>
          <h2>欢迎来到
            <RouterLink to="/"><i>翠狐驿</i></RouterLink>
          </h2>
        </el-form-item>
        <el-form-item>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
        </el-form-item>
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="account">
          <el-input v-model="userInfo.account" value="cdshi0001" :prefix-icon="User" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
          v-model="userInfo.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="agree">
          <el-checkbox  size="large" v-model="userInfo.agree">
            我已同意隐私条款和服务条款
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="button" type="primary" auto-insert-space
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login-page {
  height: 100vh;
  background-color: #fff;
  .bg {
    background: url('@/assets/login_bg.jpg') no-repeat center / cover;;
    border-radius: 0 20px 20px 0;
  }
  .title {
    width: 700px;
    height: 400px;
    color: #fff;
    h1 {
      margin-top: 200px;
      font-size: 40px;
      font-family:'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    }
    h1:nth-child(2) {
      margin-top: 150px;
      margin-left: 150px;
    }
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    h2 {
      letter-spacing: 3px;
      font-weight: 400;
      i {
        color: #27ba9b;
      }
    }
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
    .entry {
    width: 120px;
    margin-bottom: 30px;
    margin-left: 250px;
    font-size: 16px;

      i {
        font-size: 14px;
        color: $xtxColor;
        letter-spacing: -5px;
      }
  }
  }
}

</style>